package com.jk.smartelderlycare.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.jk.smartelderlycare.dto.SysGoods;
import com.jk.smartelderlycare.dto.SysProcurement;
import com.jk.smartelderlycare.dto.SysRole;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.service.SysProcurementServiceFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysProcurementController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/12/5 15:11
 */
@Slf4j
@Api(tags = "购货单管理")
@RestController
@RequestMapping(value = "procurement")
public class SysProcurementController {

    @Autowired
    private SysProcurementServiceFeign sysProcurementServiceFeign;
    /**
     * @description: 后端分页单列表
     * @param: [sysProcurement, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysProcurement>>
     * @author hhk
     * @date: 2024/12/5 15:16
     */
    @ApiOperation(value = "后端分页单列表",httpMethod = "POST",notes = "后端分页单列表")
    @PostMapping(value = "findSysProcurementPaginationList")
    private ResponseResult<PagedResponse<SysProcurement>> findSysProcurementPaginationList(@RequestBody SysProcurement sysProcurement, @RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200, "查询成功", sysProcurementServiceFeign.findSysProcurementPaginationList(sysProcurement, pageNum, pageSize));
    }
    /**
     * @description: 新增购货单
     * @param: [sysProcurement]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/12/5 17:18
     */
    @ApiOperation(value = "新增购货单",httpMethod = "POST",notes = "新增购货单")
    @PostMapping(value = "addSysProcurement")
    public ResponseResult addSysProcurement(@RequestBody SysProcurement sysProcurement){
        sysProcurementServiceFeign.addProcurement(sysProcurement);
        return new ResponseResult(200,"新增购货单成功");
    }

    /**
     * @description: 删除
     * @param: [sysProcurement]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/12/5 17:18
     */
    @ApiOperation(value = "删除",httpMethod = "DELETE",notes = "删除")
    @DeleteMapping(value = "deleteSysProcurementById")
    public ResponseResult deleteSysProcurementById(@RequestParam(value = "documentId") Long documentId){
        try {
            sysProcurementServiceFeign.deleteSysProcurementById(documentId);
            return new ResponseResult(200,"删除购货单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除购货单失败");
        }

    }
    /**
     * @description: 审核
     * @param: [documentId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/12/5 17:21
     */
    @ApiOperation(value = "审核",httpMethod = "PUT",notes = "审核")
    @PutMapping(value = "upStatusById")
    public ResponseResult upStatusById(@RequestParam(value = "documentId") Long documentId){
        try {
            sysProcurementServiceFeign.upStatusById(documentId);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }

    }
    /**
     * @description: 未审核
     * @param: [documentId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/12/5 17:22
     */
    @ApiOperation(value = "未审核",httpMethod = "PUT",notes = "未审核")
    @PutMapping(value = "downStatusById")
    public ResponseResult downStatusById(@RequestParam(value = "documentId") Long documentId){
        try {
            sysProcurementServiceFeign.downStatusById(documentId);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }

    }
    /** 
     * @description: 关闭
     * @param: [documentId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/12/5 20:58
     */ 
    @ApiOperation(value = "关闭",httpMethod = "PUT",notes = "关闭")
    @PutMapping(value = "ShutdownStatusById")
    public ResponseResult ShutdownStatusById(@RequestParam(value = "documentId") Long documentId){
        try {
            sysProcurementServiceFeign.ShutdownStatusById(documentId);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }

    }
    
    

    /**
     * @description: 信息导出
     * @param: [video, response]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 11:38
     */
    @ApiOperation(value = "信息导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody SysProcurement sysProcurement, HttpServletResponse response){
        List<SysProcurement> ProcurementList = sysProcurementServiceFeign.findSysProcurement();

        //.xlsx格式
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        response.setContentType("application/octet-stream; charset=UTF-8");


        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("信息表");
            //设置标题
            params.setTitle("信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, SysProcurement.class, ProcurementList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    @ApiOperation(value = "角色信息导入", httpMethod = "POST", notes = "信息导入")
    @PostMapping(value = "importExcel")
    public ResponseResult importExcel(@RequestPart("file") MultipartFile file) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(2);
            List<SysProcurement> list = ExcelImportUtil.importExcel(file.getInputStream(), SysProcurement.class, params);
            list.forEach(sysProcurement -> {

                sysProcurementServiceFeign.addProcurement(sysProcurement);
            });
            return new ResponseResult(200, "导入用户信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "导入用户信息失败！");
        }
    }























}
